var rtimers = 120;
var rtipId;
$(function(){
	
	/* 	$('.scy_main_right .mobile_test').on('click', function(){
			$('.popup').show();
			$('.pop_up_bg').show();
		}); */
		
	    $('.close').on('click', function(){
			$('.popup').hide();
			$('.pop_up_bg').hide();
		});
		
		
	});
/**
 * 发送验证码
 */
function sendMsg(id,sId){
	rtimers = 120;

	var newMobile = $.trim($("#newMobile").val());
	var _url = ctx_root+"/account/smsAction_smsSend.action;";
	if(id=="_login"){
		_url = ctx_root+"/account/smsAction_smsSendAccount.action;";
	}else if(id=="bindingStep1"){//旧手机发送验证码
		_url = ctx_root+"/account/smsAction_smsSendOldPhone.action;";
	}else if(id=="bindingStep2"){//新手机发送验证码
		if($("#temp").text()!=""){
			return;//校验手机号没通过
		}else if($.trim($("#newMobile").val())==""){
			$("#temp").remove();
			$("#newMobile").after("<p  id='temp' class='err_tips l'>请输入新手机号</p>");
			return;//校验手机号没通过
		}
		_url = ctx_root+"/account/smsAction_smsSendNewPhone.action;";
	}else if(id=="lowMoneyCode"){
		if($("#lowMoneyInputTemp").text()!=""){
			return;//输入正确的金额
		}
	}
	$("#"+sId+"").removeAttr("onclick");
	rtipId = window.setInterval('rtimer('+id+','+sId+');', 1000);
	$.ajax({
        type: "POST",
        url: _url,
        data: {"newMobile":newMobile,"type":id} ,
        dataType:'text',
        success: function(tt) {
        	var json = eval(tt);
	       	 if(json==null||json==""){
	    		 return;
	    	 }else{
	    		 $.each(json, function (index) { 
	    			 var isOk = json[index].isOk;
    				 if(isOk=="false"){
    					 layer.alert("验证码发送失败请稍后再试",8);
    				 }else{
    					 $("#"+sId+"").replaceWith("<a class='send l' id='"+sId+"' href='javascript:void(0);' >"+rtimers+"秒后重新获取验证码</a>");
    				 }
	    		 });
	    	 }
        }
	});
	
}

/*重置定时器*/
function resetTimer(id){
		  if(id!="bankShow"){
			  rtimers=120;
		  }
		  window.clearInterval(rtipId);
		  
		  if(id=="_login" || id=="bindingStep1" || id=="bindingStep2"){
			  $("#"+id+"Send").replaceWith("<a class='send l' id='"+id+"Send' href='javascript:void(0);' onclick=\"sendMsg('"+id+"','"+id+"Send');\">免费获取验证码</a>");
		  }
		  else{
			  $("#"+id+"Send").replaceWith("<a class='send l' id='"+id+"Send' href='javascript:void(0);' onclick=\"sendMsg('"+id+"Code','"+id+"Send');\">免费获取验证码</a>");
		  }
}

/**
 * 定时器
 * @param userId
 * @param id
 */
function rtimer(id,sId) {
	sId = sId.id;
	id = id.id;
	if (rtimers > 0) {
		$("#"+sId+"").html(rtimers + "秒后重新获取验证码");
		$("#"+sId+"").removeAttr("onclick");
		rtimers--;
	} else {
		window.clearInterval(rtipId);
//		$("#"+sId+"").html("重新发送验证码");
//		$("#"+sId+"").attr("onclick","sendCode("+userId+",'"+id+"','"+sId+"')");
		$("#"+sId+"").replaceWith("<a class='send l' id='"+sId+"' href='javascript:void(0);' onclick=\"sendMsg('"+id+"','"+sId+"');\">重新获取验证码</a>");
	}
}

/**
 * 倒计时定时器
 */
function sendCode(id,sId){
	//验证手机号码
	if ($("#"+sId+"").html() == "重新发送验证码" || $("#"+sId+"").html() == "免费获取验证码") {
		sendMsg(id,sId);
	}
}

/*div层拖动*/
function moveEvent(e,id){
	var isIE = (document.all)?true:false;
	drag = true;
	xx=isIE?event.x:e.pageX;
	yy=isIE?event.y:e.pageY;
	L = document.getElementById(id).offsetLeft;
	T = document.getElementById(id).offsetTop;
    $(".input").focusin(function (){
    	$("#"+id+"").removeAttr("onMouseDown");
    	drag = false;
    	return;
    });
	document.onmousemove = function(e) {
		if (drag) {
			x=isIE?event.x:e.pageX;		if(x<0)x=0;
			y=isIE?event.y:e.pageY;		if(y<0)y=0;
			document.getElementById(id).style.left = L-xx+245+x;//+245为手动矫正
			if(id=="bank"){
				document.getElementById(id).style.top  = T-yy+100+y;//新增银行卡+100手动矫正	
			}else{
				document.getElementById(id).style.top  = T-yy+y;
			}
		}
	}
	document.onmouseup=function(){
		drag = false;
	}
}


/*移除事件*/
function removeAt(){
	$("#lowMoney").removeAttr("onMouseDown");
	$("#payPsw").removeAttr("onMouseDown");
	$("#bank").removeAttr("onMouseDown");
	$("#_login").removeAttr("onMouseDown");
	$("#bankShow").removeAttr("onMouseDown");
	$("#movePay").removeAttr("onMouseDown");
	$("#bindingStep1").removeAttr("onMouseDown");
	$("#bindingStep2").removeAttr("onMouseDown");
	$("#realName").removeAttr("onMouseDown");
	$("#realNameAdd").removeAttr("onMouseDown");
	$("#withdrawPaySms").removeAttr("onMouseDown");
	$("#transferPaySms").removeAttr("onMouseDown");
	$("#buyHkb").removeAttr("onMouseDown");
	$("#joinHkb").removeAttr("onMouseDown");
	$("#changeHkb").removeAttr("onMouseDown");
}
/*加上div拖动事件*/
function removeEvent(){
	$("#lowMoney").attr("onMouseDown","moveEvent(event,'lowMoney')");
	$("#payPsw").attr("onMouseDown","moveEvent(event,'payPsw')");
	$("#bank").attr("onMouseDown","moveEvent(event,'bank')");
	$("#_login").attr("onMouseDown","moveEvent(event,'_login')");
	$("#bankShow").attr("onMouseDown","moveEvent(event,'bankShow')");
	$("#movePay").attr("onMouseDown","moveEvent(event,'movePay')");
	$("#bindingStep1").attr("onMouseDown","moveEvent(event,'bindingStep1')");
	$("#bindingStep2").attr("onMouseDown","moveEvent(event,'bindingStep2')");
	$("#realName").attr("onMouseDown","moveEvent(event,'realName')");
	$("#realNameAdd").attr("onMouseDown","moveEvent(event,'realNameAdd')");
	$("#withdrawPaySms").attr("onMouseDown","moveEvent(event,'withdrawPaySms')");
	$("#transferPaySms").attr("onMouseDown","moveEvent(event,'transferPaySms')");
	$("#buyHkb").attr("onMouseDown","moveEvent(event,'buyHkb')");
	$("#joinHkb").attr("onMouseDown","moveEvent(event,'joinHkb')");
	$("#changeHkb").attr("onMouseDown","moveEvent(event,'changeHkb')");
}
